<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;
use App\Model\{Admin,User,Config};

/**
 * @menu_cat 团队
 * @icon sunglasses
 * @sort 20
 */
class AdminsController extends AdminController{
	public function __construct(){
		parent::__construct();
		
		view()->share('admin_group', Config::get_config_bykey('admin_group'));
		view()->share('admin_status_config', Config::get_config_bykey('admin_status'));
	}
	
	public function __invoke(){
		return $this->index(func_get_args());
	}
	
	/**
	 * @menu 团队列表
	 * @author zhuyiqi
	 */
	public function index(){
		$users = DB::table('admindata')->paginate(15);
		return view('Admin.Admins.index')->with([
				'list' => $users,
		]);
	}
	
	/**
	 * @menu 新增成员
	 * #@author zhuyiqi
	 */
	public function add(){
		if(request()->isMethod('get')){
			return view('Admin.Admins.add',[
				'group'=>DB::table('admin_group')->get()->toArray(),
			]);
		}
		$this->validate(request(), [
				'name' => 'required',
				'passwd' => 'required',
				'repasswd' => ['required',"same:passwd"],
				'group' => 'required'
		]);
 		$input = request()->only(['name', 'passwd', 'group', 'rights']);
 		if(!isset($input) || empty($input['rights'])){
 			return back()->withErrors(['uid'=>trans('validation.non_rights')]);
 		}
 		if(Admin::where('username', $input['name'])->exists()){
 			return back()->withErrors(['uid'=>'该用户已经是管理员'])->withInput();
 		}
		
 		$id = Admin::insertGetId([
 				'username' => $input['name'],
 				'password'=>md5($input['passwd']),
 				'group_id' => $input['group'],
 				'rights' => join('|', $input['rights']),
 		]);
		#admin_log(['msg'=>$this->admin['name'].'新增了管理员[ '.$id.' ]']);
		return redirect('/Admins/index');
	}
	
	/**
	 * @name 编辑管理员
	 * @author liuchang
	 */
	public function edit($id){
		$admin_info = Admin::get_admin_info($id);
		if(request()->isMethod('post')){
			$admin_input = request()->input('admin');
			$rights = request()->input('rights')??[];
			$admin_input['rights'] = join('|',$rights);
			$admin_input['group_id'] = request()->input('group_id')??1;
			$article_cats = request()->input('article_cats')??[];
			$admin_input['article_cats'] = !empty($article_cats)?json_encode($article_cats):'';
			
			$video_cats = request()->input('video_cats')??[];
			$admin_input['video_cats'] = !empty($video_cats)?json_encode($video_cats):'';
			
			$admin = Admin::where('id', $id)->update($admin_input);
			return redirect("/Admins/edit/".$id);
		}

		$admin_info['article_cats'] = !empty($admin_info['article_cats'])?json_decode($admin_info['article_cats']):[];
		$admin_info['video_cats'] = !empty($admin_info['video_cats'])?json_decode($admin_info['video_cats']):[];
		$cats = DB::table('typedata')->get()->toArray();
		$cats = arrays_set_key($cats,'id');
		$video_cats = DB::table('typedata_video')->get()->toArray();
		$video_cats = arrays_set_key($video_cats,'id');
		return view('Admin.Admins.edit',[
			'admin_info'=> $admin_info,
			'cats'=>$cats,
			'video_cats'=>$video_cats,
			'group'=>DB::table('admin_group')->get()->toArray(),
		]);
	}

	/**
	 * @name 删除管理员
	 * @author wudi
	 */
	public function ajax_delete_admin($id){
		if($id==1){
			die(json_encode(['status'=>0,'info'=>'不能删除超级管理员']));
		}
		if( DB::table('admindata')->where('id',$id)->delete() ){
            die(json_encode(['status'=>1,'data'=>$id]));
        }
        die(json_encode(['status'=>0,'info'=>'fail']));
	}


}
